Motivating example

Matric pass rates

Making it interactive

Animating it

How does it work?

A slightly easier example

p <- diamonds %>% sample_n(1000) %>%
  ggplot(aes(x = x, y = price)) + geom_point() +
  labs(x = "Diamond width (x-dim)", y = "Price")
p

Make interactive with plotly

plotly tries to guess the right plot type

diamonds %>% sample_n(1000) %>%
  plot_ly(x = ~x, y = ~price)

Make interactive with plotly

Better to specify with add_[plottype]

diamonds %>% sample_n(1000) %>%
  plot_ly(x = ~x, y = ~price) %>%
  add_markers()

Make interactive with plotly

Styling options use plotly syntax

diamonds %>% sample_n(1000) %>%
  plot_ly(x = ~x, y = ~price) %>%
  add_markers(color = I("red")) %>%
  layout(xaxis = list(title = "Diamond width (x-dim)"),
         yaxis = list(title = "Price"))

Make interactive with plotly

“Aesthetic” options also use plotly syntax

diamonds %>% sample_n(1000) %>%
  plot_ly(x = ~x, y = ~price, color = ~clarity, colors = "Set2") %>%
  add_markers() %>%
  layout(xaxis = list(title = "Diamond width (x-dim)"),
         yaxis = list(title = "Price"))

Exercise

Open 03-ia-exercise.R.

Make the plot below interactive using plotly. Reproduce as much of the plot as you can (colours, titles, etc)

The easy way with ggplotly

Step 1: Make the ggplot

# with colour by clarity
p <- diamonds %>% sample_n(1000) %>%
  ggplot(aes(x = x, y = price, colour = clarity)) +
  geom_point() + geom_smooth(se = FALSE) +
  labs(x = "Diamond width (x-dim)", y = "Price") +
  scale_color_brewer(palette = "Set2")
p

The easy way with ggplotly

Step 1: Make the ggplot

The easy way with ggplotly

Step 2: ggplotly the ggplot!

ggplotly(p)

Exercise

Open 02-ia-exercise.R.

Differences between clarity groups in the previous plot might be easier to see if we plot densities rather than all points. Replot the previous plot as a 2-D density plot, coloured by clarity, using the geom_density2d geom.

Interactive maps

# one of the plots we made in the previous lecture
occupancies <- st_read("../spatial-data/data/Mongolia_SL.shp", 
    quiet = TRUE)
sights <- read.csv("../spatial-data/data/confirmed-sightings.csv")
sights <- st_as_sf(sights, coords = c("Longitude", "Latitude"), 
    crs = 4326) %>% st_transform(sights, crs = "+proj=utm +zone=48 +datum=WGS84 +units=m +no_defs")

p <- ggplot() + geom_sf(data = occupancies, aes(fill = Occ)) + 
    geom_sf(data = sights, aes(colour = Elevation)) + scale_colour_distiller(palette = "PRGn") + 
    scale_fill_distiller(palette = "YlOrRd")

Interactive maps

Interactive maps

Animation

How does it work?

Uses “frame” argument to index time

df <- data.frame(x = runif(1000), y = runif(1000), f = 1:1000)
p <- ggplot(df, aes(x, y)) + geom_point(aes(frame = f))
p

How does it work?

No frame argument for ggplot but ggplotly knows what to do

ggplotly(p)

Back to matric pass rates

p <- matrics_long %>%
  ggplot(aes(x = year, y = pass_rate, colour = province)) +
  geom_line(aes(frame = year)) + geom_point(aes(frame = year)) +
  scale_colour_brewer(palette = "Set3")
p

Back to matric pass rates

ggplotly(p)

More advanced example

pgap <- ggplot(
  gapminder,
  aes(x = gdpPercap, y=lifeExp, size = pop, colour = country)) +
  geom_point(aes(frame = year), show.legend = FALSE, alpha = 0.7) +
  scale_color_viridis_d() +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  labs(x = "GDP per capita", y = "Life expectancy")

More advanced example

pgap

More advanced example

ggplotly(pgap)

Or with gganimate package

pgap + transition_time(year) +
  labs(title = "Year: {frame_time}")

How does it work?

Adds transitions - functions that determine how the plot data should be distributed over frames

  • transition_time
  • transition_states
  • transition_reveal
  • transition_manual
  • & others

Uses tweening - calculates intermediate data for a smooth transition between the states

Easing controls how these intermediate data are calculated (defaults to linear)

transition_states

Transition between several distinct stages of the data

  • states: column with state levels.
  • transition_length: length of the transition between states. +state_length: length of the pause at the states.
  • wrap = TRUE: if TRUE the last state will be transitioned into the first.

transition_states

pgap <- ggplot(
  gapminder,
  aes(x = gdpPercap, y=lifeExp, size = pop, colour = year)) +
  geom_point(show.legend = FALSE, alpha = 0.7) +
  scale_color_viridis_c() +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  labs(x = "GDP per capita", y = "Life expectancy") +
  transition_states(continent) +
  labs(title = "Continent: {closest_state}")

transition_states

Something not quite right

transition_states

Need to add group = continent to aes()

pgap2 <- ggplot(
  gapminder,
  aes(x = gdpPercap, y=lifeExp, size = pop, colour = year, group = continent)) +
  geom_point(show.legend = FALSE, alpha = 0.7) +
  scale_color_viridis_c() +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  labs(x = "GDP per capita", y = "Life expectancy") 

transition_states

transition_reveal

Reveal data along a given dimension

p <- ggplot(matrics_long, aes(year, pass_rate, group = province)) +
  geom_line() +
  geom_point(size = 2)
p + transition_reveal(along = year)

transition_manual

Maps a variable to a specific frame in the animation, with no tweening of data.

pgap + transition_manual(year) +
  labs(title = "Year: {current_frame}")

transition_manual

pgap + transition_manual(year) +
  labs(title = "Year: {current_frame}") +
  facet_grid(~continent)

Output and save animations

animate(pgap, renderer = gifski_renderer(loop = TRUE))

# defaults to last animation if `animation` option not set
anim_save(filename = "my_gap_gif.gif", animation = pgap)

gganimate control settings

  • transition_*(): how data should change across time.
  • view_*(): how scales should change along the animation.
  • shadow_*(): how data from other points in time should be presented.
  • enter_*()/exit_*(): how new data should appear and how old data should disappear.
  • ease_aes(): how aesthetics should be eased during transitions.

Exercise

Exercise 03-ia-exercise.R takes you through various control settings for how gganimate plots transitions.

Exercise 04-ia-exercise.R illustrates a spatial version of the gapminder animations we made earlier, so that we can plot animations of e.g. changes in life expectancy on top of a map.

Additional resources